package prassein.rsm.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import edu.emory.mathcs.backport.java.util.Collections;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import prassein.rsm.dto.DownloadChoice;
import prassein.rsm.entity.base.Resume;

public class WriteExcel {

	private WritableCellFormat timesBoldUnderline;
	private WritableCellFormat times;
	private String inputFile;
	
	private DownloadChoice filter;
	
	private List<Resume> resumeList;

	public WriteExcel(List<Resume> resumeList) {
		this.resumeList = resumeList;
	}

	public void setOutputFile(String inputFile) {
		File file = new File(inputFile);
		
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		} else {
			File[] files = file.getParentFile().listFiles();
			if (files != null && files.length > 100) {
				int count = files.length > 20 ? 20 : files.length;
				for (int i = 0; i < count; i++) {
					files[i].delete();
				}
			}
		}
		
		this.inputFile = inputFile;
	}

	public void write() throws IOException, WriteException {
		File file = new File(inputFile);
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
		workbook.createSheet("简单信息列表", 0);
		WritableSheet excelSheet = workbook.getSheet(0);
		createLabel(excelSheet);
		createContent(excelSheet);

		workbook.write();
		workbook.close();
	}

	private void createLabel(WritableSheet sheet) throws WriteException {
		// Lets create a times font
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		times = new WritableCellFormat(times10pt);
		// Lets automatically wrap the cells
		times.setWrap(true);

		// Create create a bold font with unterlines
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		// Lets automatically wrap the cells
		timesBoldUnderline.setWrap(true);

		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

		// Write a few headers
		
		int count = 0;
		if (filter.getState() != null && filter.getState().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "状态");
			count++;
		}
		if (filter.getName() != null && filter.getName().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "姓名");
			count++;
		}
		if (filter.getSex() != null && filter.getSex().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "性别");
			count++;
		}
		if (filter.getAge() != null && filter.getAge().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "年龄");
			count++;
		}
		if (filter.getNationality() != null && filter.getNationality().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "国籍");
			count++;
		}
		if (filter.getAddress() != null && filter.getAddress().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "现住址");
			count++;
		}
		if (filter.getContactNumber() != null && filter.getContactNumber().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "联系电话");
			count++;
		}
		if (filter.getIndustry() != null && filter.getIndustry().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "所属行业");
			count++;
		}
		if (filter.getWorkingPlace() != null && filter.getWorkingPlace().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "目前工作地点");
			count++;
		}
		if (filter.getTypeOfWork() != null && filter.getTypeOfWork().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "工作类型");
			count++;
		}
		if (filter.getCorporation() != null && filter.getCorporation().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "公司名称");
			count++;
		}
		if (filter.getPosition() != null && filter.getPosition().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "职位名称");
			count++;
		}
		if (filter.getWorkSeniority() != null && filter.getWorkSeniority().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "工作年限");
			count++;
		}
		if (filter.getAnnualsalary() != null && filter.getAnnualsalary().equalsIgnoreCase("true")) {
			addCaption(sheet, count, 0, "目前年薪");
		}
	}

	private void createContent(WritableSheet sheet) throws WriteException, RowsExceededException {
		Collections.reverse(resumeList);
		for (int i = 0; i < resumeList.size(); i++) {
			int count = 0;
			if (filter.getState() != null && filter.getState().equalsIgnoreCase("true")) {
				if (resumeList.get(i).getState() != null) {
					addLabel(sheet, count, i + 1, resumeList.get(i).getState() == 1 ? "关闭" : "跟进中");
				} else {
					addLabel(sheet, count, i + 1, "关闭");
				}
				
				count++;
			}
			if (filter.getName() != null && filter.getName().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getName());
				count++;
			}
			if (filter.getSex() != null && filter.getSex().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getSex());
				count++;
			}
			if (filter.getAge() != null && filter.getAge().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getDateOfBirth());
				count++;
			}
			if (filter.getNationality() != null && filter.getNationality().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getNationality());
				count++;
			}
			if (filter.getAddress() != null && filter.getAddress().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getAddress());
				count++;
			}
			if (filter.getContactNumber() != null && filter.getContactNumber().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getContactNumber());
				count++;
			}
			if (filter.getIndustry() != null && filter.getIndustry().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getIndustry());
				count++;
			}
			if (filter.getWorkingPlace() != null && filter.getWorkingPlace().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getWorkingPlace());
				count++;
			}
			if (filter.getTypeOfWork() != null && filter.getTypeOfWork().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getTypeOfWork());
				count++;
			}
			if (filter.getCorporation() != null && filter.getCorporation().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getCorporation());
				count++;
			}
			if (filter.getPosition() != null && filter.getPosition().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getPosition());
				count++;
			}
			if (filter.getWorkSeniority() != null && filter.getWorkSeniority().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getWorkSeniority());
				count++;
			}
			if (filter.getAnnualsalary() != null && filter.getAnnualsalary().equalsIgnoreCase("true")) {
				addLabel(sheet, count, i + 1, resumeList.get(i).getAnnualsalary());
			}
		}
	}

	private void addCaption(WritableSheet sheet, int column, int row, String s)
			throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, timesBoldUnderline);
		sheet.addCell(label);
	}

//	private void addNumber(WritableSheet sheet, int column, int row,
//			Integer integer) throws WriteException, RowsExceededException {
//		Number number;
//		number = new Number(column, row, integer, times);
//		sheet.addCell(number);
//	}

	private void addLabel(WritableSheet sheet, int column, int row, String s)
			throws WriteException, RowsExceededException {
		Label label;
		label = new Label(column, row, s, times);
		sheet.addCell(label);
	}

	public DownloadChoice getFilter() {
		return filter;
	}

	public void setFilter(DownloadChoice filter) {
		this.filter = filter;
	}

//	public static void main(String[] args) throws WriteException, IOException {
//		WriteExcel test = new WriteExcel();
//		test.setOutputFile("c:/lars.xls");
//		test.write();
//		System.out
//				.println("Please check the result file under c:/temp/lars.xls ");
//	}
}